package com.grj.park.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.grj.park.common.R;
import com.grj.park.domain.User;
import com.grj.park.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("user")
public class UserController {

    private final UserService userService;

    @GetMapping
    public R<List<User>> getUsers(){
        return R.ok(userService.list());
    }

    @PostMapping
    public R<?> saveUser(@RequestBody User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User one = userService.getOne(queryWrapper);
        if (one != null){
            return R.fail("用户名已存在");
        }
        userService.save(user);
        return R.okMsg("添加用户成功");
    }
}
